package playground.fibu.bean;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import playground.fibu.ejb.AccountBO;
import playground.fibu.ejb.entity.Account;

public class AccountConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		Account acc = null;
		try {
			acc = AccountBO.findByPrimaryKey(value);
		} catch (Exception e) {
			throw new ConverterException(e);
		}
		return acc;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		String result = null;
		try {
			Account acc = (Account) value;
			result = String.valueOf(acc.getId());
		} catch (Exception e) {
			throw new ConverterException(e);
		}
		return result;
	}

}
